package com.liudada.cloud.controller;

import com.liudada.cloud.annotation.SysLog;
import com.liudada.cloud.entities.Pay;
import com.liudada.cloud.entities.PayDTO;
import com.liudada.cloud.resp.ResultData;
import com.liudada.cloud.service.PayService;
import io.micrometer.core.instrument.util.TimeUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Timer;

/**
 * @author liushuntao
 * @date 2024/4/15
 */
@RestController
@Tag(name = "支付微服务模块", description = "支付CRUD")
@RequiredArgsConstructor
@SysLog("支付微服务模块")
@RequestMapping("/pay")
public class PayController {

    private final PayService payService;





    @PostMapping(value = "/add")
    @Operation(summary = "新增", description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        System.out.println(pay.toString());
        int i = payService.add(pay);
        return ResultData.success("成功插入记录，返回值：" + i);
    }

    @DeleteMapping(value = "/del/{id}")
    @Operation(summary = "删除", description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        int i = payService.delete(id);
        return ResultData.success(i);
    }

    @PutMapping(value = "/update")
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);

        int i = payService.update(pay);
        return ResultData.success("成功修改记录，返回值：" + i);
    }

    @GetMapping(value = "/get/{id}")
    @Operation(summary = "按照ID查流水", description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        try {
            Thread.sleep(62000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if (id == -4) throw new RuntimeException("id不能为负数");
        Pay pay = payService.getById(id);
        return ResultData.success(pay);
    }

    //全部查询getall作为家庭作业

    @GetMapping(value = "/get/info")
    private String getInfoByConsul(@Value("${liudada.info}") String liudada, @Value("${server.port}") String port,@Value("${lll.zzz}") String zzz) {
        try {
            Thread.sleep(62000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "atguiguInfo: " + liudada + "\t" + "port: " + port +  "\t" + "zzz: " + zzz;
    }
}
